﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class HD
    {

        public StringBuilder application_identifier { get; set; } 
        public StringBuilder universal_ID { get; set; }
        public StringBuilder universal_ID_type { get; set; } 



        public HD()
        {
            application_identifier = new StringBuilder();
            universal_ID_type = new StringBuilder();
            universal_ID = new StringBuilder();
        }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }
        
        public int fill(string source,int temp)
        {
            int x = 0, i;
                for(i=temp;i<source.Length;i++)
                {
                    if (check(source, i) != null)
                    {
                        application_identifier.Insert(application_identifier.Length, source[i]);
                        universal_ID.Insert(universal_ID.Length, source[i]);
                        universal_ID_type.Insert(universal_ID_type.Length, source[i]);
                        return i - 1;
                    }
                    else if (source[i] == '^')
                        x++;
                    else if (source[i] == '|')
                    {
                        return i;
                    }
                    else if (x == 0)
                    {
                        application_identifier.Insert(application_identifier.Length, source[i]);
                    }
                    else if (x == 1)
                    {
                        universal_ID.Insert(universal_ID.Length, source[i]);
                    }
                    else if (x == 2)
                    {
                        universal_ID_type.Insert(universal_ID_type.Length, source[i]);
                    }

                }

            return i;
        }

        public string tostring()
       {
            string hd_string = "";

            if (application_identifier != null)
                hd_string += application_identifier;
            if (universal_ID .Length!= 0)
                hd_string += "^" + universal_ID;
            if (universal_ID_type.Length != 0)
                hd_string += "^" + universal_ID_type;
            return hd_string;
        }

        
    }
}
